package com.goods.business.service.imp;

import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.SupplierVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Resource
    private SupplierMapper supplierMapper;

    /* @Override
     public List<SupplierVO> findSupplierList(String name) {
         List<Supplier> supplierList;
         if (name.equals("")){
             supplierList = supplierMapper.selectAll();
         }else {
             Supplier supplier = new Supplier();
             supplier.setName(name);
             supplierList = supplierMapper.selectByName(name);
         }
         List<SupplierVO> collect = supplierList.stream().map(supplier -> {
             SupplierVO supplierVO = new SupplierVO();
             try {
                 //复制 被复制的数据类型要完全包含复制的
                 BeanUtils.copyProperties(supplier, supplierVO);
             } catch (Exception e) {
                 e.printStackTrace();
             }
             return supplierVO;
         }).collect(Collectors.toList());
         return collect;
     }*/
    //查询
    @Override
    public List<SupplierVO> getSupplierList(Supplier supplier) {
        List<Supplier> supplierList = supplierMapper.selectBySupplier(supplier);
        List<SupplierVO> collect = supplierList.stream().map(supplier1 -> {
            SupplierVO supplierVO = new SupplierVO();
                //复制 被复制的数据类型要完全包含复制的
                BeanUtils.copyProperties(supplier1, supplierVO);
            return supplierVO;
        }).collect(Collectors.toList());
        return collect;

    }

    //添加
    @Override
    public Integer addSupplier(Supplier supplier) {
        Date date = new Date();
        supplier.setCreateTime(date);
        supplier.setModifiedTime(date);
        return supplierMapper.insert(supplier);
    }

    //回显
    @Override
    public SupplierVO edit(Long id) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier, supplierVO);
        return supplierVO;
    }

    @Override
    public Integer update(Long id, Supplier supplier) {
        supplier.setId(id);
        Date date = new Date();
        supplier.setModifiedTime(date);
        return supplierMapper.updateByPrimaryKeySelective(supplier);
    }

    @Override
    public Integer delete(Long id) {
        return supplierMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<SupplierVO> findAll() {
        List<Supplier> supplierList = supplierMapper.selectAll();
        List<SupplierVO> collect = supplierList.stream().map(supplier -> {
            SupplierVO supplierVO = new SupplierVO();
            BeanUtils.copyProperties(supplier, supplierVO);
            return supplierVO;
        }).collect(Collectors.toList());
        return collect;
    }


}
